package com.logica.mdm.webfrontend.dao.impl;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.logica.mdm.webfrontend.dao.SearchPartyDao;
import com.logica.mdm.webfrontend.dao.beans.PartyResultBean;

/**
 * Implementation using NamedParameterJdbcTemplate
 */
@Repository
public class JdbcSearchPartyDaoImpl implements SearchPartyDao{

	private NamedParameterJdbcTemplate jdbcTemplate;

	@Autowired
	public void setDataSource(DataSource dataSource) {
	    this.jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
	}
	
	/**
	 * {@inheritDoc}
	 */
	public List<PartyResultBean> searchParty(String sqlQuery, Map<String,?> sqlParams) {
		return this.jdbcTemplate.query(sqlQuery, sqlParams, new PartyMapper());
	}
	
	/**
	 * {@inheritDoc}
	 */
	public Integer countParty(String sqlQuery, Map<String,?> sqlParams) {
		return this.jdbcTemplate.queryForInt(sqlQuery, sqlParams);
	}
	
	/**
	 * Inner class defining the mapping between the database and PartyResultBean
	 */
	private static final class PartyMapper implements RowMapper<PartyResultBean> {
		public PartyResultBean mapRow(ResultSet rs, int rowNum) throws SQLException {
			PartyResultBean party = new PartyResultBean();
			party.setRowidObject(rs.getString("ROWID_OBJECT"));
			party.setPartyType(rs.getString("PARTY_TYPE"));
			party.setPartyTypeDisp(rs.getString("PARTY_TYPE_DISP"));
			party.setGenderCd(rs.getString("GENDER_CD"));
			party.setGenderDisp(rs.getString("GENDER_DISP"));
			party.setLegalFormCd(rs.getString("LEGAL_FORM_CD"));
			party.setLibelleLegalForm(rs.getString("LIBELLE_LEGAL_FORM"));
			party.setNamePrefixCd(rs.getString("NAME_PREFIX_CD"));
			party.setLastName(rs.getString("LAST_NAME"));
			party.setFirstName(rs.getString("FIRST_NAME"));
			party.setBirthdate(rs.getTimestamp("BIRTHDATE"));
			BigDecimal unitNumber = rs.getBigDecimal("UNIT_NUMBER");
			if (unitNumber != null) {
				party.setUnitNumber(unitNumber.toBigInteger());
			}
			BigDecimal floorNumber = rs.getBigDecimal("FLOOR_NUMBER");
			if (floorNumber != null) {
				party.setFloorNumber(floorNumber.toBigInteger());
			}
			party.setStreetNumber(rs.getString("STREET_NUMBER"));
			party.setStreetType(rs.getString("STREET_TYPE"));
			party.setStreetName(rs.getString("STREET_NAME"));
			party.setAddressLine1(rs.getString("ADDRESS_LINE_1"));
			party.setAddressLine2(rs.getString("ADDRESS_LINE_2"));
			party.setAddressLine3(rs.getString("ADDRESS_LINE_3"));
			party.setAddressLine5(rs.getString("ADDRESS_LINE_5"));
			party.setPostalCd(rs.getString("POSTAL_CD"));
			party.setCityName(rs.getString("CITY_NAME"));
			party.setCountryName(rs.getString("COUNTRY_NAME"));
			party.setCountryCode(rs.getString("COUNTRY_CODE"));
			party.setHexacleVoie(rs.getString("HEXACLE_VOIE"));
			party.setHexacleLocalite(rs.getString("HEXACLE_LOCALITE"));
			party.setHomePhoneNumber(rs.getString("HOME_PHONE_NUMBER"));
			party.setCellPhoneNumber(rs.getString("CELL_PHONE_NUMBER"));
			party.setWorkPhoneNumber(rs.getString("WORK_PHONE_NUMBER"));
			party.setFaxPhoneNumber(rs.getString("FAX_PHONE_NUMBER"));
			party.setElectronicAddress(rs.getString("ELECTRONIC_ADDRESS"));
			party.setFlagPartenaire(rs.getString("FLAG_PARTENAIRE"));
			party.setCrmId(rs.getString("CRM_ID"));
			party.setLogementId(rs.getString("LOGEMENT_ID"));
			party.setServiceId(rs.getString("SERVICE_ID"));
	        return party;
	    }        
	}
}
